from nsga2.nsga2 import NSGA2
from problems.zdt3 import ZDT3
import numpy as np
import matplotlib.pyplot as plt

def run_zdt3():
    optimizer = NSGA2(pop_size=100, n_var=30, pc=0.9, pm=0.1, eta_c=20, eta_m=20)
    problem = ZDT3()

    [optimum_x, optimum_fx] = optimizer.run(problem, max_gen=500, seed=42)

    print("Final fitness values (optimum_fx):", optimum_fx)

    optimum_fx_array = np.array(optimum_fx)

    f1 = optimum_fx_array[:, 0]
    f2 = optimum_fx_array[:, 1]

    print("Range of f1:", f1.min(), "to", f1.max())
    print("Range of f2:", f2.min(), "to", f2.max())

    # plt.figure(figsize=(8, 6))  
    plt.scatter(f1, f2, c='grey', marker='o', label='NSGA-II Solutions')
    plt.grid(True, linestyle='--', alpha=0.5)
    plt.xlabel('f1')
    plt.ylabel('f2')
    plt.title('NSGA-II Optimization Results for ZDT3')
    plt.legend()
    plt.tight_layout()
    plt.show()


if __name__ == "__main__":
    run_zdt3()